package com.siysoft.tc.admin.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.siysoft.tc.admin.service.IAdminService;
import com.siysoft.tc.base.bean.to.Manager;
import com.siysoft.util.BaseUtil;
import com.siysoft.util.IMap;

/**
 * <p>Title: AdminController</p>
 * <p>Description:管理员功能</p>
 * <p>Company: SiySoft</p>
 * @author    zhouyu
 * @date       2015年6月5日
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminController extends BaseUtil {
	@Autowired
	IAdminService adminService;
	
	//登录跳转
	@RequestMapping(value = "/")
	public String toAdmin(HttpServletRequest request) throws Exception{
		return "redirect:/admin/login.htm";
	}
	
	//主页跳转
	@RequestMapping(value = "/index")
	public String toIndex(HttpServletRequest request) throws Exception{
		List<Map<String, String>>countTeacher=adminService.getTeacherCount(null);
		request.setAttribute("countTeacher", countTeacher);
		List<Map<String, String>>teachers=adminService.getTeacher(null);
		request.setAttribute("teachers", teachers);
		return "/admin/index";
	}
	
	//登录跳转
	@RequestMapping(value = "/login")
	public String toAdminLogin(HttpServletRequest request) throws Exception{
		return "forward:/WEB-INF/pages/admin/adminLogin.jsp";
	}
	
	//管理员信息修改页面跳转
	@RequestMapping(value = "/adminRevise")
	public String toAdeminRevise(HttpServletRequest request) throws Exception{
		IMap iMap = new IMap(request);
		Map<String, String> reviseAdmin = adminService.toReviseAdmin(iMap);
		request.setAttribute("list", reviseAdmin);
		return "/admin/adminRevise";
	}
	
	//管理员修改管理员信息
	@RequestMapping(value = "/reviseInfo")
	public String reviseInfo(HttpServletRequest request) throws Exception{
		IMap iMap = new IMap(request);
		iMap.put("manager_id", iMap.getInt("manager_id"));
		iMap.put("username", iMap.getString("username"));
		adminService.reviseAdmin(iMap);
		return "redirect:/admin/adminUserCheck.htm";
	}
	
	//管理员添加页面跳转
	@RequestMapping(value = "/adminAppend")
	public String toAdeminAppend(){
		
		return "/admin/adminAppend";
	}
	
	//添加管理员功能
	@RequestMapping(value = "/appendAdmin")
	public String appendAdmin(HttpServletRequest request) throws Exception{
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		Manager manager = new Manager();
		Md5PasswordEncoder md5 = new Md5PasswordEncoder();
		md5.setEncodeHashAsBase64(false);
        String pwd = md5.encodePassword(password,username);
        manager.setPassword(pwd);
        manager.setUsername(username);
        adminService.saveAdmin(manager);
		return "redirect:/admin/adminUserCheck.htm";
	}
	
	//管理员课程查看功能
	@RequestMapping(value = "/adminUserCheck")
	public String adminClassCheck(HttpServletRequest request) throws Exception{
		IMap iMap = new IMap(request);
		List<Map<String,String>> seekInfo = adminService.seekInfo(iMap);
		request.setAttribute("list", seekInfo);
		return "/admin/adminUserCheck";
	}
	
	//跳转管理员修改密码页面
	@RequestMapping(value = "/toRevisePassword")
	public String revisePassword(HttpServletRequest request) throws Exception{
		Manager manager = (Manager) request.getSession().getAttribute("user");
		request.setAttribute("manager", manager);
		return "/admin/revisePassword";
	}
	
	//跳转管理员修改密码页面
		@RequestMapping(value = "/toRevisePasswords")
		public String revisePasswords(HttpServletRequest request) throws Exception{
			IMap iMap=new IMap(request);
			Manager manager= adminService.findManager(iMap);
			request.setAttribute("manager", manager);
			return "/admin/revisePassword";
		}
	
	//管理员修改密码
	@RequestMapping(value = "/managerRevisePassword")
	public String managerRevisePassword(HttpServletRequest request) throws Exception{
		String managerId = request.getParameter("manager_id");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		Manager manager = new Manager();
		Md5PasswordEncoder md5 = new Md5PasswordEncoder();
		md5.setEncodeHashAsBase64(false);
        String pwd = md5.encodePassword(password,username);
        manager.setManagerId(Integer.parseInt(managerId));
        manager.setPassword(pwd);
        manager.setUsername(username);
        adminService.managerRevisePassword(manager);
		return "redirect:/admin/index.htm";
	}
	
	//添加管理员AJAX验证
	@RequestMapping(value = "/checkName")
	public void checkName(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String username=null;
		try{
			username=request.getParameter("username");
		}catch(Exception e){
			e.printStackTrace();
		}
		Manager manager=null;
		try {
			//检查重复性
			this.adminService.checkName(username);
			
		} catch (Exception e) {
			this.failure(response, "当前账号已存在");
			return;
		}
	}
}
